package com.yc.cloud.admin.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yc.cloud.admin.dto.request.SysApplicationCreateParam;
import com.yc.cloud.admin.dto.request.SysApplicationUpdateParam;
import com.yc.cloud.admin.service.SysApplicationService;
import com.yc.cloud.api.vo.SysApplicationVo;
import com.yc.cloud.common.basic.dto.BaseQueryParam;
import com.yc.cloud.common.basic.utils.AppIdGenerator;
import com.yc.cloud.common.oss.service.OssFileService;
import com.yc.cloud.entity.SysApplication;
import com.yc.cloud.mapper.SysApplicationMapper;
import lombok.val;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 系统应用 服务实现类
 * </p>
 *
 * @author Yancy
 * @since 2024-04-20
 */
@Service
public class SysApplicationServiceImpl extends ServiceImpl<SysApplicationMapper, SysApplication> implements SysApplicationService {

    private final OssFileService ossFileService;

    public SysApplicationServiceImpl(OssFileService ossFileService) {
        this.ossFileService = ossFileService;
    }

    @Override
    public IPage<SysApplicationVo> list(BaseQueryParam queryParam) {
        val query = new QueryWrapper<SysApplication>();
        IPage<SysApplication> page;
        //排序
        if (StrUtil.isNotEmpty(queryParam.getSortBy())) {
            if (BaseQueryParam.ORDER_DESC.equals(queryParam.getOrder())) {
                query.orderByDesc(queryParam.getSortBy());
            } else if (BaseQueryParam.ORDER_ASC.equals(queryParam.getOrder())) {
                query.orderByAsc(queryParam.getSortBy());
            }
        }
        //模糊查询
        if (StrUtil.isNotEmpty(queryParam.getKeyword())) {
            query.and(queryWrapper -> {

            });
        }
        //分页
        if (queryParam.getPage()) {
            page = new Page<>(queryParam.getPageNum(), queryParam.getPageSize(), true);
        } else {
            page = new Page<>(queryParam.getPageNum(), baseMapper.selectCount(query), true);
        }
        val result = baseMapper.selectPage(page, query);
        return result.convert(this::convertVo);
    }

    @Override
    public SysApplication create(SysApplicationCreateParam param) {
        val sysapplication = new SysApplication();
        BeanUtils.copyProperties(param, sysapplication);
        sysapplication.setAppId(generateAppId());
        val saveFlag = save(sysapplication);
        return saveFlag ? sysapplication : null;
    }

    @Override
    public SysApplication get(Long id) {
        return getById(id);
    }

    @Override
    public boolean delete(Long id) {
        return removeById(id);
    }

    @Override
    public SysApplication update(Long id, SysApplicationUpdateParam param) {
        val sysapplication = new SysApplication();
        BeanUtils.copyProperties(param, sysapplication);
        sysapplication.setId(id);
        val updateFlag = updateById(sysapplication);
        return updateFlag ? sysapplication : null;
    }


    @Override
    public SysApplicationVo convertVo(SysApplication sysapplication) {
        if (sysapplication == null) {
            return null;
        }
        val vo = new SysApplicationVo();
        BeanUtils.copyProperties(sysapplication, vo);
        vo.setIconUrl(ossFileService.getOssFileUrl(String.valueOf(sysapplication.getIcon())));
        return vo;
    }

    @Override
    public List<SysApplicationVo> convertVo(List<SysApplication> SysApplicationList) {
        if (CollUtil.isNotEmpty(SysApplicationList)) {
            return SysApplicationList.stream().map(this::convertVo).collect(Collectors.toList());
        }
        return new ArrayList<>();
    }

    private String generateAppId() {
        var appId = AppIdGenerator.generateAppId(true);
        while (getByAPPId(appId) != null) {
            appId = AppIdGenerator.generateAppId(true);
        }
        return appId;
    }

    private SysApplication getByAPPId(String appId) {
        return baseMapper.selectOne(new QueryWrapper<SysApplication>().eq(SysApplication.APP_ID, appId));
    }
}
